/*
ehs - 2010-04-03
flickr JavaScript "class"

2010-04-03
----------
- encapsulated auth.js functions
- clear token when check returns that it is invalid
*/

function Flickr()
{
	var authData = null;


	function isAuthorized()
		{ return authData != null; }


	function setAuthData(newAuthData)
	{
		authData = newAuthData;
		// fixme: does jQuery have a better way to check multiple layers like this?
		var authToken = getAuthToken();
		if (authToken != "")
			$.cookies.set("auth_token", authToken);
		else
			$.cookies.del("auth_token");
	}


	function getAuthToken()
	{
		if (authData != null && authData.token != null && authData.token._content != null)
			return authData.token._content;
		return "";
	}


	function getUserId()
	{
		if (authData != null && authData.user != null && authData.user.nsid != null)
			return authData.user.nsid;
		return "";
	}


	function getUserName()
	{
		if (authData != null && authData.user != null && authData.user.username != null)
			return authData.user.username;
		return "";
	}


	// clientCallback: function(isAuthorized)
	function checkAuth(clientCallback)
	{
		if (isAuthorized()) {
			callback(true);
			return;
		}

		// need to retrieve auth_token from cookie because we are initializing authentication
		var authToken = $.cookies.get("auth_token");
		if (authToken != null && authToken != "") {
			$.get("/WebTag/proxy.php", {
				method: "flickr.auth.checkToken",
				auth_token: authToken
			}, function(data, textStatus) {
				finishAuth(data, textStatus, clientCallback);
			}, "json");
			return;
		}

		var frob = getParameterByName("frob");
		if (frob != null && frob != "") {
			$.get("/WebTag/proxy.php", {
				method: "flickr.auth.getToken",
				frob: frob
			}, function(data, textStatus) {
				finishAuth(data, textStatus, clientCallback);
			}, "json");
			return;
		}

		clientCallback(false);
	}


	// clientCallback: function(isAuthorized)
	function finishAuth(data, textStatus, clientCallback)
	{
		setAuthData(textStatus == "success" && data.stat == "ok" ? data.auth : null);
		clientCallback(isAuthorized());
	}


	// clientCallback: function(setMap{name:id})
	function getSets(clientCallback)
	{
		// fixme: need to further abstract this AJAX call
		$.get("/WebTag/proxy.php", {
			method: "flickr.photosets.getList",
			auth_token: getAuthToken()
		}, function(data, textStatus) {
			finishGetSets(data, textStatus, clientCallback);
		}, "json");
	}


	// clientCallback: function(setMap{name:id})
	function finishGetSets(data, textStatus, clientCallback)
	{
		if (textStatus != "success" || data.stat != "ok") {
			clientCallback({});
			return;
		}

		var setMap = {};
		for (var i = 0; i < data.photosets.photoset.length; i++) {
			var photoset = data.photosets.photoset[i];
			setMap[photoset.title._content] = photoset.id;
		}
		clientCallback(setMap);
	}


	function selectSet(setId, clientCallback)
	{
		setTimeout(clientCallback, 2000);
	}


	var publicObj = {
		isAuthorized: isAuthorized,
		checkAuth: checkAuth,
		getUserName: getUserName,
		getSets: getSets,
		selectSet: selectSet
	};
	return publicObj;
}

